package com.voltmao.webpro.utils.obd;

import com.voltmao.webpro.utils.JsonUtil;
import com.voltmao.webpro.utils.PropertiesFileUtils;
import com.voltmao.webpro.utils.http.HttpClientUtil;
import com.voltmao.webpro.utils.obd.po.OBDUserInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * 中胜OBD服务层
 * Created by zhouyang on 2017/1/13.
 */
public class OBDUtils {

    /**
     * 中胜用户 注册并获取用户信息
     *
     * @param obdUserName
     * @return
     */
    public static synchronized OBDUserInfo getObdUserInfo(final String obdUserName) {
        //获取接口数据
        final Map<String, String> proMap = PropertiesFileUtils.getProperties("OBDServer.properties");
        String sendUrl = proMap.get("domain") + proMap.get("registerUserUrl");

        //注册中胜用户
        Map resultRegisterMap = HttpClientUtil.sendGetToMap(sendUrl, new HashMap<String, Object>() {{
            put("custName", obdUserName);
            put("account", obdUserName);
            put("password", proMap.get("pwd"));
            put("carBrandId", "13");
            put("carSeriesId", "21995");
            put("carModelId", "21998");
            put("chePai", "");
            put("tele", "");
        }});

//        String resultStr=JsonUtil.ToMap(resultMap.get("body")+"").get("result")+"";
//        System.out.println(Escape.unescape(resultStr));

        //获取中胜用户信息
        String loginUrl = proMap.get("domain") + proMap.get("appLoginUrl");
        Map resultLoginMap = HttpClientUtil.sendGetToMap(loginUrl, new HashMap<String, Object>() {{
            put("userName", obdUserName);
            put("password", proMap.get("pwd"));
            put("loginType", "1");
        }});

        String reStr = (resultLoginMap.get("body") + "").replaceAll("/basedata/hc_bqww.png", "");
        return (OBDUserInfo) JsonUtil.ParsJSON(reStr, OBDUserInfo.class);
    }
}
